/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
jQuery(document).ready(function($){
     
    $('.basic-device-selector').change(function(e){
        
        var selector = $(this);
        $.post( "/api/request_saving_data/",
        { 
            func: 'getSavingItems', 
            basic_item_id: selector.val()
        },
        function(data) {
            if(data.status == 'OK'){
                selector.parents('.form-table').find('.saving-device-selector').html(data.content);
            } 
        });
        
    });
   
    $('.add-saving-device').on('click', function(e) {
        e.preventDefault();
        var table = $(this).parents('.saving-devices-table');
        var newRow = table.find('.saving-device-empty-row').clone(true);
        newRow.removeClass('saving-device-empty-row');
        newRow.addClass('saving-device-data-row');
        newRow.insertBefore(table.find('.add-saving-device-row'));
        return false;
    });
    
    $('.remove-saving-device').on('click', function(e) {
        e.preventDefault();
        $(this).parent().parent().remove();
        return false;
    });
    
    $('#water-add-basic-device').on('click', function(e) {
        e.preventDefault();
        var newForm = $('#water-basic-device-empty-row').clone(true);
        newForm.removeAttr('id');
        newForm.find('.saving-device-counter-data').val(1);
        newForm.removeClass('basic-device-empty-row');
        newForm.insertBefore($('#water-add-basic-device-row'));
    });
    
    $('#energy-add-basic-device').on('click', function(e) {
        e.preventDefault();
        var newForm = $('#energy-basic-device-empty-row').clone(true);
        newForm.removeAttr('id');
        newForm.find('.saving-device-counter-data').val(1);
        newForm.removeClass('basic-device-empty-row');
        newForm.insertBefore($('#energy-add-basic-device-row'));
    });
    
    $('.remove-basic-device').on('click', function(e) {
        e.preventDefault();
        $(this).parent().parent().remove();
    });
    
    $('.metabox_submit').click(function(e) {
        e.preventDefault();
        
        $('#modify_water').val(1);
        $('#modify_energy').val(1);
        
        $('.form-table').each(function() {
            
            var saving_devices_counter = $(this).find('.saving-device-counter-data').val();
            
            if(saving_devices_counter != 0){
                
                var saving_devices_input = $(this).find('.saving-device-values-data');
                var saving_devices_table = $(this).find('.saving-devices-table');
                var saving_devices_string = '';

                saving_devices_table.find('tr.saving-device-data-row').each(function() {
                    id = $(this).find('.saving-device-selector').val();
                    ratio = $(this).find('.saving-device-ratio').val();
                    if(id != 0){
                        saving_devices_string += id + '+' + ratio + '/';
                    }
                });

                saving_devices_input.val(saving_devices_string);
                
            }
            
        });
        
        $('#publish').click();
    });
       
    //mascaras
    $('.mask_usage').mask('000');
    $('.mask_decimal').keyup(function(){
        var val = $(this).val();
        if(isNaN(val)){
            val = val.replace(/[^0-9\.]/g,'');
            if(val.split('.').length>2)
                val =val.replace(/\.+$/,"");
        }
        $(this).val(val);
    });
    
});

